/*
 * EmissionsViewerView.java
 */

package emissionsviewer;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;

/**
 * The application's main frame.
 */
public class EmissionsViewerView extends JFrame implements ActionListener {

    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	public EmissionsViewerView() {
    	super("Emission Viewer");
    	
    	setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    	refLibButtonText = "Choose Reference Lib";
    	dataButtonText   = "Choose Data";
    	plotEmissionsButtonText = "Plot Emissions!";
    	initComponents();
    	
        chooseRefButton.addActionListener(this);
        chooseDataButton.addActionListener(this);
        plotButton.addActionListener(this);

        pack();

    }

	public void actionPerformed(ActionEvent ae) {
		if (ae.getSource() instanceof JButton) {
			JButton button = (JButton)ae.getSource();
			if (button.getText() == refLibButtonText || button.getText() == dataButtonText) {
				// open file chooser
				JFileChooser fc = new JFileChooser();
				int retval = fc.showOpenDialog(this);
				File file = fc.getSelectedFile();

				if (retval == JFileChooser.APPROVE_OPTION) {
					if (file.canRead()) {
						if (button.getText() == refLibButtonText) {
		                    app.setRefFile(file);
		                    EmissionsViewerView.this.refFileLabel.setText(file + " loaded");
						} else {
		                    app.setDataFile(file);
		                    EmissionsViewerView.this.dataLabel.setText(file + " loaded");
						}
					}
				}
			} else if (button.getText() == plotEmissionsButtonText) {
		        if (checkAllSet()) {
		            if (emitPlot == null) {
		                emitPlot = new EmitPlot();
		                emitPlot.setVisible(true);
		                emitPlot.setSize(800, 800);
		            }
		        } else {
		            System.out.println("Something isn't set!");
		        }
			} else {
				System.out.println("Unknown button pressed: " + button.getText());
			}
		}
	}

    public boolean checkAllSet() {
        boolean err = false;
        if (!app.getDataFile().canRead()) {
            System.err.println("Cannot read data file " + app.getDataFile());
            err = true;
        }
        if (!app.getRefFile().canRead()) {
            System.err.println("Cannot read reference file " + app.getRefFile());
            err = true;
        }

        HashMap<Integer, JCheckBox> ltcbs = new HashMap<Integer, JCheckBox>();
        ltcbs.put(1, ltcb_1);
        ltcbs.put(2, ltcb_2);
        ltcbs.put(3, ltcb_3);
        ltcbs.put(4, ltcb_4);
        ltcbs.put(5, ltcb_5);
        ltcbs.put(6, ltcb_6);
        ltcbs.put(7, ltcb_7);
        ltcbs.put(8, ltcb_8);
        ltcbs.put(9, ltcb_9);
        ltcbs.put(10, ltcb_10);

        ArrayList<Integer> lts = new ArrayList<Integer>();
        Iterator<Integer> it = ltcbs.keySet().iterator();
        Integer cb_id; JCheckBox cb;
        while (it.hasNext()) {
            cb_id = (Integer)it.next();
            cb = (JCheckBox)ltcbs.get(cb_id);
            if (cb.isSelected()) {
                lts.add((Integer)cb_id);
//System.out.println("id: " + cb_id + ", text: " + cb.getText());
            }
        }
        app.setLinkTypes(lts);
        if (lts.isEmpty()) {
            System.err.println("No link types were chosen");
            err = true;
        }

        return !err;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    private void initComponents() {

        mainPanel = new javax.swing.JPanel();
        chooseRefButton = new javax.swing.JButton();
        refFileLabel = new javax.swing.JLabel();
        chooseDataButton = new javax.swing.JButton();
        dataLabel = new javax.swing.JLabel();
        emitComboBox = new javax.swing.JComboBox();
        plotButton = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        ltcb_1 = new javax.swing.JCheckBox();
        ltcb_2 = new javax.swing.JCheckBox();
        ltcb_3 = new javax.swing.JCheckBox();
        ltcb_4 = new javax.swing.JCheckBox();
        ltcb_5 = new javax.swing.JCheckBox();
        ltcb_10 = new javax.swing.JCheckBox();
        ltcb_9 = new javax.swing.JCheckBox();
        ltcb_8 = new javax.swing.JCheckBox();
        ltcb_7 = new javax.swing.JCheckBox();
        ltcb_6 = new javax.swing.JCheckBox();
        linkTypeLabel = new javax.swing.JLabel();

        mainPanel.setName("mainPanel");

        chooseRefButton.setText(refLibButtonText);
        chooseRefButton.setName("chooseRefButton");

        refFileLabel.setText("/home/matt/gaea-matt/karman/ref.csv loaded");
        refFileLabel.setName("refFileLabel");

        chooseDataButton.setText(dataButtonText);
        chooseDataButton.setName("chooseDataButton");

        dataLabel.setText("/home/matt/gaea-matt/karman/data.out loaded");
        dataLabel.setName("dataLabel");

        emitComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21" }));
        emitComboBox.setName("emitComboBox"); // NOI18N
        emitComboBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                emitComboBoxActionPerformed(evt);
            }
        });

        plotButton.setText(plotEmissionsButtonText);
        plotButton.setName("plotButton"); // NOI18N

        jLabel1.setText("Emission");
        jLabel1.setName("jLabel1"); // NOI18N

        jPanel1.setBackground(new java.awt.Color(202, 200, 198));
        jPanel1.setName("jPanel1"); // NOI18N

        ltcb_1.setBackground(new java.awt.Color(249, 229, 212));
        ltcb_1.setSelected(true);
        ltcb_1.setText("1");
        ltcb_1.setName("ltcb_1"); // NOI18N

        ltcb_2.setText("2");
        ltcb_2.setName("ltcb_2"); // NOI18N

        ltcb_3.setText("3");
        ltcb_3.setName("ltcb_3"); // NOI18N

        ltcb_4.setText("4");
        ltcb_4.setName("ltcb_4"); // NOI18N

        ltcb_5.setText("5");
        ltcb_5.setName("ltcb_5"); // NOI18N

        ltcb_10.setText("10");
        ltcb_10.setName("ltcb_10"); // NOI18N

        ltcb_9.setText("9");
        ltcb_9.setName("ltcb_9"); // NOI18N

        ltcb_8.setText("8");
        ltcb_8.setName("ltcb_8"); // NOI18N

        ltcb_7.setText("7");
        ltcb_7.setName("ltcb_7"); // NOI18N

        ltcb_6.setText("6");
        ltcb_6.setName("ltcb_6"); // NOI18N

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(ltcb_1)
                    .addComponent(ltcb_2)
                    .addComponent(ltcb_3)
                    .addComponent(ltcb_4)
                    .addComponent(ltcb_5))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 102, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(ltcb_6)
                    .addComponent(ltcb_7)
                    .addComponent(ltcb_8)
                    .addComponent(ltcb_9)
                    .addComponent(ltcb_10))
                .addGap(101, 101, 101))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(ltcb_6)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(ltcb_7)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(ltcb_8)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(ltcb_9)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(ltcb_10))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(ltcb_1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(ltcb_2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(ltcb_3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(ltcb_4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(ltcb_5)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        linkTypeLabel.setText("Link types");
        linkTypeLabel.setName("linkTypeLabel"); // NOI18N

        javax.swing.GroupLayout mainPanelLayout = new javax.swing.GroupLayout(mainPanel);
        mainPanel.setLayout(mainPanelLayout);
        mainPanelLayout.setHorizontalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(mainPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(mainPanelLayout.createSequentialGroup()
                        .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(chooseRefButton)
                            .addComponent(chooseDataButton)
                            .addComponent(jLabel1)
                            .addComponent(linkTypeLabel))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(dataLabel)
                            .addComponent(refFileLabel)
                            .addComponent(emitComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(plotButton))
                .addContainerGap(31, Short.MAX_VALUE))
        );
        mainPanelLayout.setVerticalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(mainPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(chooseRefButton)
                    .addComponent(refFileLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(chooseDataButton)
                    .addComponent(dataLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(emitComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(mainPanelLayout.createSequentialGroup()
                        .addComponent(linkTypeLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 136, Short.MAX_VALUE)
                        .addComponent(plotButton))
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        add(mainPanel);
        
    }

    private void emitComboBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_emitComboBoxActionPerformed
        EmissionsViewerApp app = (EmissionsViewerApp)EmissionsViewerApp.getInstance();
        Integer I = Integer.parseInt(this.emitComboBox.getSelectedItem().toString());
        app.setEmitType(I);
    }//GEN-LAST:event_emitComboBoxActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton chooseDataButton;
    private javax.swing.JButton chooseRefButton;
    private javax.swing.JLabel dataLabel;
    private javax.swing.JComboBox emitComboBox;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JLabel linkTypeLabel;
    private javax.swing.JCheckBox ltcb_1;
    private javax.swing.JCheckBox ltcb_10;
    private javax.swing.JCheckBox ltcb_2;
    private javax.swing.JCheckBox ltcb_3;
    private javax.swing.JCheckBox ltcb_4;
    private javax.swing.JCheckBox ltcb_5;
    private javax.swing.JCheckBox ltcb_6;
    private javax.swing.JCheckBox ltcb_7;
    private javax.swing.JCheckBox ltcb_8;
    private javax.swing.JCheckBox ltcb_9;
    private javax.swing.JPanel mainPanel;
    private javax.swing.JButton plotButton;
    private javax.swing.JLabel refFileLabel;
    // End of variables declaration//GEN-END:variables

    private JFrame emitPlot;
    private String refLibButtonText, dataButtonText, plotEmissionsButtonText;
    protected static EmissionsViewerApp app = EmissionsViewerApp.getInstance();;
}
